const DS = require('./DataStruct')
const DC = require('./DataCenter')
module.exports = {
    //数据同步 移动
    //客户端人物每移动一步会向服务器发一次请求
    //告诉其他客户端该客户端移动了一步
    //(DS.RequestCode.GAME, DS.GameAction.MOVE, msg)
    //msg.code
    //msg.msg
    //msg.id
    Move: (connect) => {
        let msg = new Object()
        DC.RoomList.forEach(room => {
            if (room.id === connect.roomId) {
                msg.code = 200
                msg.id = connect.id
                msg.msg = connect.name + '移动了一步'
                let sendMsg = DS.packData(DS.RequestCode.GAME, DS.GameAction.MOVE, msg)
                DC.BoardCast(false, sendMsg, room.member, connect)
            }
        })
    },
    //游戏结束
    //第一个玩家到达后开始倒计时
    //倒计时结束，开始计算排名，结算成绩
    Over: (connect) => {
        connect.endTime = Date.now()
        let msg = new Object()
        DC.RoomList.forEach(room => {
            if (room.id === connect.roomId) {
                //给到达终点的玩家一个到达的时间
                room.member.forEach(player => {
                    if (connect.id === player.id) {
                        player.endTime = connect.endTime
                        player.isFinish = true
                    }
                })
                room.overMember++
                //第一个玩家到达后开始倒计时
                if (room.overMember === 1) {
                    let i = 5
                    let timer = setInterval(() => {
                        msg.code = 200
                        msg.msg = i
                        msg.state = 0
                        let sendMsg = DS.packData(DS.RequestCode.GAME, DS.GameAction.OVER, msg)
                        DC.BoardCast(true, sendMsg, room.member)
                        i--
                        if (i <= 0) {
                            clearInterval(timer)
                            //如果没有达到终点，需要将它的时间设置大一些
                            room.member.forEach(player => {
                                if (!player.endTime) {
                                    player.endTime = 9999999999999
                                    player.isFinish = false
                                }
                            })
                            //排序（选择排序）通过时间排名
                            //将时间小的放前面
                            for (let j = 0; j < room.member.length; j++) {
                                for (let k = j + 1; k < room.member.length; k++) {
                                    if (room.member[j].endTime > room.member[k].endTime) {
                                        let val = room.member[j]
                                        room.member[j] = room.member[k]
                                        room.member[k] = val
                                    }
                                }
                            }
                            //重构数据
                            const temp = new Object()
                            temp.roomId = room.id
                            temp.roomName = room.name
                            temp.member = []

                            room.member.forEach(member => {
                                const tempMember = new Object()
                                tempMember.id = member.id
                                tempMember.name = member.name
                                tempMember.isReady = member.isReady
                                tempMember.isFinish = member.isFinish
                                tempMember.endTime = member.endTime
                                temp.member.push(tempMember)
                            })
                            msg.code = 200
                            msg.msg = '游戏结算排名'
                            msg.state = 1
                            msg.rankList = temp
                            let sendMsg = DS.packData(DS.RequestCode.GAME, DS.GameAction.OVER, msg)
                            DC.BoardCast(true, sendMsg, room.member)
                        }
                    }, 1000)
                }
            }
        })

    }
}